package com.xc.ksxt.controller;

import com.alibaba.fastjson.JSON;
import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.TagGroup;
import com.xc.ksxt.entity.TagGroupEntity;
import com.xc.ksxt.service.TagGroupService;
import com.xc.ksxt.vo.TagGroupVO;
import com.xc.sys.entity.SysDictEntity;
import com.xc.sys.service.SysDictService;
import com.xc.sys.service.SysUserService;
import com.xc.sys.vo.SysUserVO;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/tagGroup")
@CrossOrigin
public class TagGroupController {

    @Autowired
    private TagGroupService tagGroupService;
    private TagGroup TAGGROUP = TagGroup.TAG_GROUP.as("TAGGROUP");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<TagGroupVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<TagGroupVO> ltr = new LayuiTableResponse<>();
        Condition cond = TAGGROUP.DEL_FLAG.notEqual(-1);
        String name = request.getParameter("name");
        String keywords = request.getParameter("keywords");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if (StringUtils.isNotBlank(name)) {
            cond = cond.and(TAGGROUP.NAME.like("%"+name+"%"));
        }
        if (StringUtils.isNotBlank(keywords)) {
            cond = cond.and(TAGGROUP.NAME.like("%"+keywords+"%"));
        }
        Long count = tagGroupService.count(cond);
        ltr.setCount(count);
        PageInfo<TagGroupEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> list = tagGroupService.select(cond, pageInfo,TAGGROUP.ORDERNO.asc(),TAGGROUP.NAME.asc(),TAGGROUP.CREATE_TIME.desc());
        List<TagGroupVO> tagGroupEntityList = list.into(TagGroupVO.class);
        for (TagGroupVO tagGroupEntity : tagGroupEntityList){
            SysUserVO user = SysUserService.getFrameCaches(tagGroupEntity.getCreateUserId());
            tagGroupEntity.setCreateUserName(user==null?"":user.getName());
            SysDictEntity dict = SysDictService.getFromCaches("rvalid-state," + tagGroupEntity.getState());
            tagGroupEntity.setStateTxt(dict==null?"":dict.getName());
        }
        ltr.setData(tagGroupEntityList);
        return ltr;
    }

    @RequestMapping({"/getById"})
    public TagGroupEntity getById(Integer id) {
        TagGroupEntity entity = tagGroupService.getEntityById(id);
        return entity;
    }

    @RequestMapping({"/save"})
    public ResponseEntity<TagGroupEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
        ResponseEntity<TagGroupEntity> res = new ResponseEntity<>();
        TagGroupEntity tagGroupEntity = Helper.map2Bean(new TagGroupEntity(), params);
        tagGroupService.save(tagGroupEntity);
        return res;
    }
    @RequestMapping({"/delete"})
    public LayuiTableResponse<TagGroupEntity> delete(@RequestParam("ids[]") Integer[] ids) {
        LayuiTableResponse<TagGroupEntity> ltr = new LayuiTableResponse<>();
        for (Integer id : ids) {
            TagGroupEntity m = tagGroupService.getEntityById(id);
            m.setDelFlag(-1);
            tagGroupService.update(m);
        }
        return ltr;
    }

}
